from flask import Flask, render_template, request, send_from_directory
import pdfkit

app = Flask(__name__)

@app.route('/')
def index():
    return render_template('index.html')

@app.route('/generate_pdf', methods=['POST'])
def generate_pdf():
    text = request.form['text']

    import homework
    # 生成PDF文件
    homework.text2pdf(text, 'static/pdfs')

    # 返回PDF文件链接
    return f'PDF文件已生成，请点击下载：<a href="/download_pdf">下载</a>'

@app.route('/download_pdf')
def download_pdf():
    return send_from_directory('static/pdfs', 'output.pdf', as_attachment=True)

if __name__ == '__main__':
    app.run(host="0.0.0.0")
